package com.example.springcloudwinter.controller;

import com.example.springcloudwinter.dto.ResultEntity;
import com.example.springcloudwinter.service.PaymentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping ("/payment")
@RefreshScope
public class PaymentController {
    
    @Autowired
    PaymentService paymentService;
    @Autowired
    private Environment env;
    @Value ("${test.refresh}")
    private String refresh;
    
    @PostMapping ("/pay/{orderId}/{type}")
    public ResultEntity<?> payOrder (@PathVariable ("orderId") int id, @PathVariable ("type") int type, @RequestParam ("userId") String userId) {
        return paymentService.payOrder(id, userId);
    }
    
    @GetMapping ("/test/refresh")
    public ResultEntity<String> testConfigRefresh () {
        Logger logger = LoggerFactory.getLogger("payment");
        logger.info("/test/refresh");
        return new ResultEntity<String>().setCode(200)
                .setDetail("from@env" + env.getProperty("test.refresh") + " from@Value:" + refresh);
    }
}
